//
//  CTLanguageVC.m
//  digitalCurrency
//
//  Created by apple on 2019/6/10.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTLanguageVC.h"
#import "YLTabBarController.h"
#import "CTLoginVC.h"
#import "MineViewController.h"
@interface CTLanguageVC ()
@property (nonatomic, copy) NSArray *titles;
@end

@implementation CTLanguageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    self.viewTitle.text = LocalizationKey(@"language");
    _titles = @[@"简体中文",@"English"];
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 50;
    self.tableView.separatorColor = [UIColor clearColor];

}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = self.view.frame;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"langcell"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"langcell"];
    }

    UIView *lineView = [[UIView alloc]init];
    [cell.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(17);
        make.right.equalTo(-17);
        make.bottom.equalTo(0);
        make.height.equalTo(0.5);
    }];
    lineView.backgroundColor = tTableViewCellLine;
    
    cell.textLabel.text = _titles[indexPath.row];
    cell.textLabel.textColor = [UIColor colorWithHEX:maincolor];
    
    NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:@"language"];
    if (!language) {
        NSString *lang = [[NSUserDefaults standardUserDefaults] objectForKey:LanguageChange];
        if ([lang isEqualToString:@"en"]) {
            language = @"english";
        }
        else {
            language = @"chinese";
        }
    }
    if ([LocalizationKey(language) isEqualToString:cell.textLabel.text]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle == UITableViewCellAccessoryCheckmark) {
        cell.selectionStyle = UITableViewCellAccessoryNone;
    }
    else {
        cell.selectionStyle = UITableViewCellAccessoryCheckmark;
    }
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    if ([cell.textLabel.text isEqualToString:@"English"]) {
        [ud setObject:@"english" forKey:@"language"];
        [ChangeLanguage setUserlanguage:@"en"];
    }
    else {
        [ud setObject:@"chinese" forKey:@"language"];
        [ChangeLanguage setUserlanguage:@"zh-Hans"];
    }
    
    [ud synchronize];

    if ([self.isLoginStr isEqualToString:@"1"]) {
        CTLoginVC *login = [[CTLoginVC alloc] init];
               YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:login];
        APPLICATION.window.rootViewController = nav;
    }
    else{
        YLTabBarController*tabBarController = (YLTabBarController*)APPLICATION.window.rootViewController;
           [tabBarController resettabarItemsTitle];
        
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            YLTabBarController *tabbarVC = [[YLTabBarController alloc] init];
            delegate.window.rootViewController = tabbarVC;
            for (UINavigationController *navi in tabbarVC.childViewControllers) {
                for (UIViewController *vc in navi.childViewControllers) {
                    if ([vc isKindOfClass:[MineViewController class]]) {
                        
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }
            }
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
